//
//  GameScene.swift
//  MyFirstSpriteKit
//
//  Created by 龙 轶群 on 15/9/15.
//  Copyright (c) 2015年 龙 轶群. All rights reserved.
//

import SpriteKit

class GameScene: SKScene {
    
    var user:SKSpriteNode!
    var moving:SKNode!
    
    override func didMoveToView(view: SKView) {
        
        // 全局重力
        self.physicsWorld.gravity = CGVectorMake(0.0, -5.0)
        
        // 背景
        let skyColor = SKColor(red: 81.0/255.0, green: 192.0/255.0, blue: 201.0/255.0, alpha: 1.0)
        self.backgroundColor = skyColor
        
        // 设置主体
        let userTexture1 = SKTexture(imageNamed: "bird-01")
        userTexture1.filteringMode = .Nearest
        
        
        let userTexture2 = SKTexture(imageNamed: "bird-02")
        userTexture2.filteringMode = .Nearest
        
        let anim = SKAction.animateWithTextures([userTexture1, userTexture2], timePerFrame: 0.2)
        let flap = SKAction.repeatActionForever(anim)
        
        user = SKSpriteNode(texture: userTexture1)
        user.setScale(2.0)
        user.position = CGPoint(x: self.frame.size.width * 0.35, y:self.frame.size.height * 0.6)
        user.runAction(flap)
        
        user.physicsBody = SKPhysicsBody(circleOfRadius: user.size.height / 2.0)
        user.physicsBody!.dynamic = true
        user.physicsBody!.allowsRotation = false
        self.addChild(user)
        
        // 设置地面
        let groundTexture = SKTexture(imageNamed: "gggg")
        groundTexture.filteringMode = SKTextureFilteringMode.Nearest
        let ground = SKNode()
        
        let moveGroundSprite = SKAction.moveByX(-groundTexture.size().width * 2.0, y: 0, duration: NSTimeInterval(0.02 * groundTexture.size().width * 2.0))
        let resetGroundSprite = SKAction.moveByX(groundTexture.size().width * 2.0, y: 0, duration: 0.0)
        let moveGroundSpritesForever = SKAction.repeatActionForever(SKAction.sequence([moveGroundSprite,resetGroundSprite]))
        
        moving = SKNode()
        self.addChild(moving)
        
        for var i:CGFloat = 0; i < 2.0 + self.frame.size.width / ( groundTexture.size().width * 2.0 ); ++i {
            let sprite = SKSpriteNode(texture: groundTexture)
            sprite.setScale(2.0)
            //sprite.xScale = 2.0
            sprite.position = CGPoint(x: i * sprite.size.width, y: sprite.size.height / 2.0)
            sprite.runAction(moveGroundSpritesForever)
            moving.addChild(sprite)
        }
        
        ground.position = CGPoint(x: 0, y: groundTexture.size().height)
        ground.physicsBody = SKPhysicsBody(rectangleOfSize: CGSize(width: self.frame.size.width, height: groundTexture.size().height * 2.0))
        ground.physicsBody!.dynamic = false
        self.addChild(ground)
        
        // 设置背景城市
        let skyTexture = SKTexture(imageNamed: "sky")
        skyTexture.filteringMode = .Nearest
        
        let moveSkySprite = SKAction.moveByX(-skyTexture.size().width * 2.0, y: 0, duration: NSTimeInterval(0.1 * skyTexture.size().width * 2.0))
        let resetSkySprite = SKAction.moveByX(skyTexture.size().width * 2.0, y: 0, duration: 0.0)
        let moveSkySpritesForever = SKAction.repeatActionForever(SKAction.sequence([moveSkySprite,resetSkySprite]))
        
        for var i:CGFloat = 0; i < 2.0 + self.frame.size.width / ( skyTexture.size().width * 2.0 ); ++i {
            let sprite = SKSpriteNode(texture: skyTexture)
            sprite.setScale(2.0)
            sprite.zPosition = -20
            sprite.position = CGPoint(x: i * sprite.size.width, y: sprite.size.height / 2.0 + groundTexture.size().height * 2.0)
            sprite.runAction(moveSkySpritesForever)
            moving.addChild(sprite)
        }
        
        // 设置障碍
        

    }
    
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        /* Called when a touch begins */
        for _ in (touches ) {
            
            // let location = touch.locationInNode(self)
            user.physicsBody!.velocity = CGVector(dx:0, dy:0)
            user.physicsBody!.applyImpulse(CGVector(dx:0, dy:30))
        }
    }
   
    override func update(currentTime: CFTimeInterval) {
        /* Called before each frame is rendered */
        user.zRotation = self.clamp(-1, max: 0.5, value: user.physicsBody!.velocity.dy * (user.physicsBody!.velocity.dy < 0 ? 0.003 : 0.001 ))
    }
    
    // TODO: Move to utilities somewhere. There's no reason this should be a member function
    func clamp(min: CGFloat, max: CGFloat, value: CGFloat) -> CGFloat {
        if( value > max ) {
            return max
        } else if( value < min ) {
            return min
        } else {
            return value
        }
    }
    
    /*
    func CGPointDistanceSquared(from from: CGPoint, to: CGPoint) -> CGFloat {
        return (from.x - to.x) * (from.x - to.x) + (from.y - to.y) * (from.y - to.y);
    }
    
    func CGPointDistance(from from: CGPoint, to: CGPoint) -> CGFloat {
        return sqrt(CGPointDistanceSquared(from: from, to: to));
    }
    */
}
